﻿using System;
using System.Collections.Generic;
using System.IO;
using NLua;

namespace Ensemble_Windows.Script
{
    /// <summary>
    ///     Mod帮助类
    /// </summary>
    public class ModHelper
    {
        /// <summary>
        ///     ModList
        /// </summary>
        public static readonly List<string> ModList = new List<string>();

        /// <summary>
        ///     寻找所有Mod
        /// </summary>
        /// <param name="path">Mod路径</param>
        /// <param name="fileName">主文件名</param>
        /// <param name="lua">Lua</param>
        /// <exception cref="ArgumentNullException">空属性</exception>
        public static void FindAllMod(string path, string fileName, Lua lua)
        {
            if (path == null) throw new ArgumentNullException("path");
            if (fileName == null) throw new ArgumentNullException("fileName");

            ModList.Clear();
            if (!Directory.Exists(path)) return;
            var files = Directory.GetFiles(path);
            foreach (var variable in files)
            {
                if (variable != fileName) continue;
                var info = new DirectoryInfo(fileName);
                ModList.Add(info.FullName);
            }
        }


        /// <summary>
        ///     获取Mod信息
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="lua">Lua</param>
        /// <returns></returns>
        public static object[] GetModInfo(string fileName, Lua lua)
        {
            try
            {
                return lua.DoFile(fileName);
            }
            catch (Exception)
            {
                // ignored
            }
            return null;
        }
    }
}